package com.ts.proj.service.impl;

import com.ts.proj.entity.ProjectEntity;
import com.ts.proj.repository.ProjectRepository;
import com.ts.proj.service.ProjectService;
import com.ts.util.EntityAttributeUtil;
import com.ts.util.PageData;

import com.ts.base.service.impl.CommonServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


//说明 字符串：，作为替换字符串 LOWERProject                Project

@Service
public class ProjectServiceImpl extends CommonServiceImpl<ProjectEntity>  implements ProjectService {

    @Autowired
    private ProjectRepository projectRepository;

    @Override
    public Specification getSpecification(PageData pageData, Class entityClass) {
        return new Specification() {
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList();
                List<String> fieldList = EntityAttributeUtil.entityAttribute(entityClass);

                preList.add(criteriaBuilder.equal(root.get("deleteFlag"),"N"));
                if(null != pageData.get("userId") && !"".equals(pageData.get("userId").toString())) {
                    preList.add(criteriaBuilder.equal(root.get("userId"), pageData.get("userId").toString()));
                }
                if(null != pageData.get("belongTo") && !"".equals(pageData.get("belongTo").toString())){
                    preList.add(criteriaBuilder.equal(root.get("belongTo"), pageData.get("belongTo").toString()));
                }
                if(null != pageData.get("proState") && !"".equals(pageData.get("proState").toString())){
                    preList.add(criteriaBuilder.equal(root.get("proState"), pageData.get("proState").toString()));
                }
                if(null != pageData.get("proName") && !"".equals(pageData.get("proName").toString())){
                    preList.add(criteriaBuilder.like(root.get("proName"), "%"+(pageData.get("proName").toString())+"%"));
                }
                if(null != pageData.get("proCategory") && !"".equals(pageData.get("proCategory").toString())){
                    preList.add(criteriaBuilder.equal(root.get("proCategory"), pageData.get("proCategory").toString()));
                }
                if(null != pageData.get("uuid") && !"".equals(pageData.get("uuid").toString())){
                    preList.add(criteriaBuilder.equal(root.get("uuid"), pageData.get("uuid").toString()));
                }
                if(null != pageData.get("proId") && !"".equals(pageData.get("proId").toString())){
                    preList.add(criteriaBuilder.like(root.get("proId"), "%"+pageData.get("proId").toString()+"%"));
                }
                if(null != pageData.get("proManager") && !"".equals(pageData.get("proManager").toString())){
                    preList.add(criteriaBuilder.like(root.get("proManager"), "%"+pageData.get("proManager").toString()+"%"));
                }
                if(null != pageData.get("proManager") && !"".equals(pageData.get("proManager").toString())){
                    preList.add(criteriaBuilder.like(root.get("proManager"), "%"+pageData.get("proManager").toString()+"%"));
                }
                if(null != pageData.get("proApprovalTime") && !"".equals(pageData.get("proApprovalTime").toString())){
                    preList.add(criteriaBuilder.equal(root.get("proApprovalTime"), pageData.get("proApprovalTime").toString()));
                }
                //决策条件
                if(null != pageData.get("proJueCeInvestType") && !"".equals(pageData.get("proJueCeInvestType").toString())){
                    preList.add(criteriaBuilder.equal(root.get("proJueCeInvestType"), pageData.get("proJueCeInvestType").toString()));
                }
                if(null != pageData.get("proJueCeInvestCorpus") && !"".equals(pageData.get("proJueCeInvestCorpus").toString())){
                    preList.add(criteriaBuilder.like(root.get("proJueCeInvestCorpus"), "%"+pageData.get("proJueCeInvestCorpus").toString()+"%"));
                }
                if(null != pageData.get("proInvestment") && !"".equals(pageData.get("proInvestment").toString())){
                    preList.add(criteriaBuilder.like(root.get("proInvestment"), "%"+pageData.get("proInvestment").toString()+"%"));
                }
                if(null != pageData.get("proJueCeReport") && !"".equals(pageData.get("proJueCeReport").toString())){
                    preList.add(criteriaBuilder.equal(root.get("proJueCeReport"), pageData.get("proJueCeReport").toString()));
                }
                //真实投资时间(proRealInvestmentTime)
                if(null != pageData.get("proRealInvestmentTime") && !"".equals(pageData.get("proRealInvestmentTime").toString())){
                    preList.add(criteriaBuilder.equal(root.get("proRealInvestmentTime"), pageData.get("proRealInvestmentTime").toString()));
                }
                if(null != pageData.get("proTuichuTime") && !"".equals(pageData.get("proTuichuTime").toString())){
                    preList.add(criteriaBuilder.equal(root.get("proTuichuTime"), pageData.get("proTuichuTime").toString()));
                }

                //退出方式（proOutWay）
                if(null != pageData.get("proOutWay") && !"".equals(pageData.get("proOutWay").toString())){
                    preList.add(criteriaBuilder.equal(root.get("proOutWay"), pageData.get("proOutWay").toString()));
                }
                if(null != pageData.get("assignmentNumber") && !"".equals(pageData.get("assignmentNumber").toString())){
                    preList.add(criteriaBuilder.equal(root.get("assignmentNumber"), pageData.get("assignmentNumber").toString()));
                }
                if(null != pageData.get("averagePrice") && !"".equals(pageData.get("averagePrice").toString())){
                    preList.add(criteriaBuilder.equal(root.get("averagePrice"), pageData.get("averagePrice").toString()));
                }
                if(null != pageData.get("totalPrice") && !"".equals(pageData.get("totalPrice").toString())){
                    preList.add(criteriaBuilder.equal(root.get("totalPrice"), pageData.get("totalPrice").toString()));
                }
                //根据时间段查询
                if(null != pageData.get("proTuichuBeginTime") && !"".equals(pageData.get("proTuichuBeginTime").toString())){
                    preList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("proTuichuTime"),pageData.get("proTuichuBeginTime").toString()));
                }

                if(null != pageData.get("proTuichuEndTime") && !"".equals(pageData.get("proTuichuEndTime").toString())){
                    preList.add(criteriaBuilder.lessThanOrEqualTo(root.get("proTuichuTime"), pageData.get("proTuichuEndTime").toString()));
                }

                if(null != pageData.get("proRealInvestmentBeginTime") && !"".equals(pageData.get("proRealInvestmentBeginTime").toString())){
                    preList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("proRealInvestmentTime"),pageData.get("proRealInvestmentBeginTime").toString()));
                }

                if(null != pageData.get("proRealInvestmentEndTime") && !"".equals(pageData.get("proRealInvestmentEndTime").toString())){
                    preList.add(criteriaBuilder.lessThanOrEqualTo(root.get("proRealInvestmentTime"), pageData.get("proRealInvestmentEndTime").toString()));
                }

                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and((Predicate[])preList.toArray(p));
            }
        };
    }

    @Override
    public List<ProjectEntity> findAll(PageData pageData, Integer start, Integer length) {
        Specification<ProjectEntity> sp = this.getSpecification(pageData,ProjectEntity.class);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return projectRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public long count(PageData pageData) {
        Specification<ProjectEntity> sp = this.getSpecification(pageData,ProjectEntity.class);
        return projectRepository.count(sp);
    }

    @Override
    public boolean deleteByIds(Iterable<ProjectEntity> iterable) {
        projectRepository.deleteInBatch(iterable);
        return false;
    }

    @Override
    public List<ProjectEntity> saveAll(List<ProjectEntity> entities) {
        return projectRepository.saveAll(entities);
    }

    @Override
    public List<ProjectEntity> findAllById(Integer[] ids) {
        List<ProjectEntity> list = projectRepository.findAllById(Arrays.asList(ids));
        if(list.size()==0){
            ProjectEntity entity = new ProjectEntity();
            list.add(entity);
        }
        return list;
    }

    @Override
    public boolean deleteAll() {
		projectRepository.deleteAll();
        return false;
    }

    @Override
    public List<PageData> findAllProvinceProjects() {
        return projectRepository.findAllProvinceProjects();
    }

    @Override
    public List<ProjectEntity> findProjectByProName(String proName) {
        return projectRepository.findProjectByProName(proName);
    }
}